<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://schemas.microsoft.com/collection/metadata/2009" xmlns:p="http://schemas.microsoft.com/livelabs/pivot/collection/2009">
  <xsl:output method="xml" indent="no"/>
  <xsl:param name="FilterNodeSet" />
  <xsl:param name="ImgBaseName" />
  <xsl:param name="DefaultImageUrl" />
  <!-- XSL transformation starts here -->
  <xsl:template match="/">
    <Collection  SchemaVersion="1" Name="People Search results">
      <xsl:call-template name="dvt_1.body"/>
    </Collection>
  </xsl:template>
  <!-- Main body template -->
  <xsl:template name="dvt_1.body">
    <xsl:call-template name="facet.categories"/>
    <xsl:variable name="Rows" select="All_Results/NumberOfResults"/>
    <Items ImgBase="{$ImgBaseName}">
      <xsl:for-each select="All_Results/Result">
        <xsl:call-template name="SingleResult" />
      </xsl:for-each>
    </Items>
  </xsl:template>
  <xsl:template name="facet.categories">
    <xsl:element name="FacetCategories" >
      <xsl:call-template name="facet.category">
        <xsl:with-param name="facet.category.name" select="'Link:'"/>
        <xsl:with-param name="facet.category.type" select="'Link'"/>
      </xsl:call-template>
      <xsl:call-template name="facet.category">
        <xsl:with-param name="facet.category.name" select="'Skills'"/>
        <xsl:with-param name="facet.category.type" select="'String'"/>
      </xsl:call-template>
      <xsl:call-template name="facet.category">
        <xsl:with-param name="facet.category.name" select="'Schools'"/>
        <xsl:with-param name="facet.category.type" select="'String'"/>
      </xsl:call-template>
      <xsl:call-template name="facet.category">
        <xsl:with-param name="facet.category.name" select="'Past projects'"/>
        <xsl:with-param name="facet.category.type" select="'String'"/>
      </xsl:call-template>
      <xsl:call-template name="facet.category">
        <xsl:with-param name="facet.category.name" select="'Interests'"/>
        <xsl:with-param name="facet.category.type" select="'String'"/>
      </xsl:call-template>
      <xsl:call-template name="facet.category">
        <xsl:with-param name="facet.category.name" select="'Responsibility'"/>
        <xsl:with-param name="facet.category.type" select="'String'"/>
      </xsl:call-template>
      <xsl:call-template name="facet.category">
        <xsl:with-param name="facet.category.name" select="'Job Title'"/>
        <xsl:with-param name="facet.category.type" select="'String'"/>
      </xsl:call-template>
      <xsl:call-template name="facet.category">
        <xsl:with-param name="facet.category.name" select="'Department'"/>
        <xsl:with-param name="facet.category.type" select="'String'"/>
      </xsl:call-template>
      <xsl:call-template name="facet.category">
        <xsl:with-param name="facet.category.name" select="'Colleague category'"/>
        <xsl:with-param name="facet.category.type" select="'String'"/>
        <xsl:with-param name="facet.category.isfiltervisible" select="'false'" />
      </xsl:call-template>
      <xsl:call-template name="facet.category">
        <xsl:with-param name="facet.category.name" select="'Work phone'"/>
        <xsl:with-param name="facet.category.type" select="'String'"/>
        <xsl:with-param name="facet.category.isfiltervisible" select="'false'" />
        <xsl:with-param name="facet.category.ismetadatavisible" select="'true'" />
        <xsl:with-param name="facet.category.iswordweelvisible" select="'true'" />
      </xsl:call-template>
      <xsl:call-template name="facet.category">
        <xsl:with-param name="facet.category.name" select="'Office number'"/>
        <xsl:with-param name="facet.category.type" select="'String'"/>
        <xsl:with-param name="facet.category.isfiltervisible" select="'false'" />
        <xsl:with-param name="facet.category.ismetadatavisible" select="'true'" />
        <xsl:with-param name="facet.category.iswordweelvisible" select="'true'" />
      </xsl:call-template>
      <xsl:call-template name="facet.category">
        <xsl:with-param name="facet.category.name" select="'Work email'"/>
        <xsl:with-param name="facet.category.type" select="'String'"/>
        <xsl:with-param name="facet.category.isfiltervisible" select="'false'" />
        <xsl:with-param name="facet.category.ismetadatavisible" select="'true'" />
        <xsl:with-param name="facet.category.iswordweelvisible" select="'true'" />
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  <!-- This template is called for each result -->
  <xsl:template name="SingleResult">
    <xsl:variable name="id" select="number(id)-1"/>

    <xsl:variable name="hasabme"  select="string-length(aboutme) &gt; 0"/>

    <xsl:variable name="itemname">
      <xsl:choose>
        <xsl:when test="string-length(preferredname) &gt; 0">
          <xsl:value-of select="preferredname"/>
        </xsl:when>
        <xsl:when test="string-length(accountname) &gt; 0">
          <xsl:value-of select="accountname"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="'unknown name'"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:element name="Item">
      <xsl:attribute name="Id">
        <xsl:value-of select="$id"/>
      </xsl:attribute>
      <xsl:attribute name="Img">
        <xsl:value-of select="concat('#', $id)"/>
      </xsl:attribute>
      <xsl:attribute name="Name">
        <xsl:value-of select="$itemname"/>
      </xsl:attribute>
      <xsl:attribute name="Href">
        <xsl:call-template name="GetPicUrl">
          <xsl:with-param name="PicUrl" select="pictureurl"/>
          <xsl:with-param name="PlaceHolderUrl" select="$DefaultImageUrl"/>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:if test="$hasabme">
        <xsl:element name="Description">
          <xsl:value-of select="aboutme"/>
        </xsl:element>
      </xsl:if>
      <xsl:element name="Facets">
        <xsl:call-template name="SingleResultInternal">
          <xsl:with-param name="id" select="$id"/>
          <xsl:with-param name="itemname" select="$itemname"/>
        </xsl:call-template>
      </xsl:element>
    </xsl:element>
  </xsl:template>
  <xsl:template name="SingleResultInternal">
    <xsl:param name="id"/>
    <xsl:param name="itemname"/>
    <xsl:variable name="currentId" select="$id"/>
    <xsl:variable name="url" select="url"/>
    <xsl:variable name="email" select="workemail"/>
    <xsl:variable name="sip" select="sipaddress"/>
    <xsl:variable name="prefix">IMNRC('</xsl:variable>
    <xsl:variable name="suffix">')</xsl:variable>
    <xsl:variable name="hassip"       select="string-length($sip) &gt; 0"/>
    <xsl:variable name="hasemail"     select="string-length($email) &gt; 0"/>
    <xsl:variable name="hasydn"       select="string-length(yomidisplayname) &gt; 0"/>
    <xsl:variable name="hasjt"        select="string-length(jobtitle) &gt; 0"/>
    <xsl:variable name="hasdp"        select="string-length(department) &gt; 0"/>
    <xsl:variable name="hasorg"       select="string-length(orgnames) &gt; 0"/>
    <xsl:variable name="haswph"       select="string-length(workphone) &gt; 0"/>
    <xsl:variable name="hasonum"      select="string-length(officenumber) &gt; 0"/>
    <xsl:variable name="hascol"       select="string-length(colleaguecategory) &gt; 0"/>
    <xsl:variable name="hasacu"       select="string-length(addtomycolleaguesurl) &gt; 0"/>
    <xsl:variable name="hashier"      select="string-length(hierarchyurl) &gt; 0"/>
    <xsl:variable name="hasresp"      select="string-length(responsibility_multival) &gt; 0"/>
    <xsl:variable name="hassk"        select="count(skills_multival) &gt; 0"/>
    <xsl:variable name="hasint"       select="count(interests_multival) &gt; 0"/>
    <xsl:variable name="hasorgparent" select="($FilterNodeSet and $FilterNodeSet/@title='OrgParentNames') or hithighlightedproperties/orgparentnames/@hashh &gt; 0"/>
    <xsl:variable name="hasmem"       select="($FilterNodeSet and $FilterNodeSet/@title='Memberships') or hithighlightedproperties/memberships/@hashh &gt; 0"/>
    <xsl:variable name="haspp"        select="count(pastprojects_multival) &gt; 0"/>
    <xsl:variable name="hasbol"       select="($FilterNodeSet and $FilterNodeSet/@title='SPShLocation') or hithighlightedproperties/baseofficelocation/@hashh &gt; 0"/>
    <xsl:variable name="hassch"       select="count(schools_multival) &gt; 0"/>
    <xsl:variable name="hassum"       select="count(hithighlightedsummary/c0) &gt; 0"/>
    <xsl:variable name="hasvlm"       select="string-length(profileviewlastmonth) &gt; 0"/>
    <xsl:variable name="hasvlw"       select="string-length(profileviewlastweek) &gt; 0"/>
    <xsl:variable name="hasquery"     select="string-length(queriesfoundyou) &gt; 0"/>
    <xsl:call-template name="facet.link">
      <xsl:with-param name="facet.name" select="'Link:'" />
      <xsl:with-param name="facet.link.title" select="$itemname" />
      <xsl:with-param name="facet.link.url" select="url" />
    </xsl:call-template>
    <xsl:if test="$hassch">
      <xsl:call-template name="facet.string">
        <xsl:with-param name="facet.name"  select="'Schools'" />
        <xsl:with-param name="facet.values" select="schools_multival" />
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$haspp">
      <xsl:call-template name="facet.string">
        <xsl:with-param name="facet.name"  select="'Past projects'" />
        <xsl:with-param name="facet.values" select="pastprojects_multival" />
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$hasint">
      <xsl:call-template name="facet.string">
        <xsl:with-param name="facet.name"  select="'Interests'" />
        <xsl:with-param name="facet.values" select="interests_multival" />
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$hassk">
      <xsl:call-template name="facet.string">
        <xsl:with-param name="facet.name"  select="'Skills'" />
        <xsl:with-param name="facet.values" select="skills_multival" />
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$hasresp">
      <xsl:call-template name="facet.string">
        <xsl:with-param name="facet.name"  select="'Responsibility'" />
        <xsl:with-param name="facet.values" select="responsibility_multival" />
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$hasjt">
      <xsl:call-template name="facet.string">
        <xsl:with-param name="facet.name"  select="'Job Title'" />
        <xsl:with-param name="facet.values" select="jobtitle" />
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$hasdp">
      <xsl:call-template name="facet.string">
        <xsl:with-param name="facet.name"  select="'Department'" />
        <xsl:with-param name="facet.values" select="department" />
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$hascol">
      <xsl:call-template name="facet.string">
        <xsl:with-param name="facet.name"  select="'Colleague category'" />
        <xsl:with-param name="facet.values" select="colleaguecategory" />
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$haswph">
      <xsl:call-template name="facet.string">
        <xsl:with-param name="facet.name"  select="'Work phone'" />
        <xsl:with-param name="facet.values" select="workphone" />
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$hasonum">
      <xsl:call-template name="facet.string">
        <xsl:with-param name="facet.name"  select="'Office number'" />
        <xsl:with-param name="facet.values" select="officenumber" />
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$hasemail">
      <xsl:call-template name="facet.string">
        <xsl:with-param name="facet.name"  select="'Work email'" />
        <xsl:with-param name="facet.values" select="workemail" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  <!-- Render Facet category -->
  <xsl:template name="facet.category">
    <xsl:param name="facet.category.name"/>
    <xsl:param name="facet.category.type"/>
    <xsl:param name="facet.category.format"/>
    <xsl:param name="facet.category.isfiltervisible"/>
    <xsl:param name="facet.category.iswordweelvisible"/>
    <xsl:param name="facet.category.ismetadatavisible"/>
    <xsl:element name="FacetCategory">
      <xsl:attribute name="Name">
        <xsl:value-of select="$facet.category.name"/>
      </xsl:attribute>
      <xsl:attribute name="Type">
        <xsl:value-of select="$facet.category.type"/>
      </xsl:attribute>
      <xsl:if test="string-length($facet.category.format) &gt; 0">
        <xsl:attribute name="Format">
          <xsl:value-of select="$facet.category.format"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="string-length($facet.category.isfiltervisible) &gt; 0">
        <xsl:attribute name="p:IsFilterVisible">
          <xsl:value-of select="$facet.category.isfiltervisible"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="string-length($facet.category.iswordweelvisible) &gt; 0">
        <xsl:attribute name="p:IsWordWheelVisible">
          <xsl:value-of select="$facet.category.iswordweelvisible"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="string-length($facet.category.ismetadatavisible) &gt; 0">
        <xsl:attribute name="p:IsMetaDataVisible">
          <xsl:value-of select="$facet.category.ismetadatavisible"/>
        </xsl:attribute>
      </xsl:if>
    </xsl:element>
  </xsl:template>
  <!-- Render string facets -->
  <xsl:template name="facet.string">
    <xsl:param name="facet.name"/>
    <xsl:param name="facet.values"/>
    <xsl:element name="Facet">
      <xsl:attribute name="Name">
        <xsl:value-of select="$facet.name"/>
      </xsl:attribute>
      <xsl:for-each select="$facet.values">
        <xsl:element name="String">
          <xsl:attribute name="Value">
            <xsl:value-of select="node()"/>
          </xsl:attribute>
        </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>
  <!-- Render link facets -->
  <xsl:template name="facet.link">
    <xsl:param name="facet.name"/>
    <xsl:param name="facet.link.title"/>
    <xsl:param name="facet.link.url"/>
    <xsl:element name="Facet">
      <xsl:attribute name="Name">
        <xsl:value-of select="$facet.name"/>
      </xsl:attribute>
      <xsl:element name="Link">
        <xsl:attribute name="Name">
          <xsl:value-of select="$facet.link.title"/>
        </xsl:attribute>
        <xsl:attribute name="Href">
          <xsl:value-of select="$facet.link.url"/>
        </xsl:attribute>
      </xsl:element>
    </xsl:element>
  </xsl:template>
  <xsl:template name="GetPicUrl">
    <xsl:param name="PicUrl"/>
    <xsl:param name="PlaceHolderUrl"/>
    <xsl:choose>
      <xsl:when test="string-length($PicUrl) &lt; 1 or starts-with($PicUrl, 'file:') or starts-with($PicUrl, '\\') or starts-with($PicUrl, '//')">
        <xsl:value-of select="$PlaceHolderUrl"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="replace-text">
          <xsl:with-param name="text" select="$PicUrl"  />
          <xsl:with-param name="replace" select="'_MThumb'" />
          <xsl:with-param name="by" select="'_LThumb'" />
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="replace-text">
    <xsl:param name="text"/>
    <xsl:param name="replace" />
    <xsl:param name="by"  />
    <xsl:choose>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text, $replace)"/>
        <xsl:value-of select="$by" disable-output-escaping="yes"/>
        <xsl:call-template name="replace-text">
          <xsl:with-param name="text" select="substring-after($text, $replace)"/>
          <xsl:with-param name="replace" select="$replace" />
          <xsl:with-param name="by" select="$by" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>